D:\a\tools.proto\tools.proto\compiler\src\compiler\util\store.rs
Line | Count | Source |
1 | | // Copyright (c) 2024, BlockProject 3D |
2 | | // |
3 | | // All rights reserved. |
4 | | // |
5 | | // Redistribution and use in source and binary forms, with or without modification, |
6 | | // are permitted provided that the following conditions are met: |
7 | | // |
8 | | // * Redistributions of source code must retain the above copyright notice, |
9 | | // this list of conditions and the following disclaimer. |
10 | | // * Redistributions in binary form must reproduce the above copyright notice, |
11 | | // this list of conditions and the following disclaimer in the documentation |
12 | | // and/or other materials provided with the distribution. |
13 | | // * Neither the name of BlockProject 3D nor the names of its contributors |
14 | | // may be used to endorse or promote products derived from this software |
15 | | // without specific prior written permission. |
16 | | // |
17 | | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
18 | | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
19 | | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
20 | | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
21 | | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
22 | | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
23 | | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
24 | | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
25 | | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
26 | | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
27 | | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | | |
29 | | use bp3d_util::index_map::IndexMap; |
30 | | use std::collections::HashMap; |
31 | | use std::rc::Rc; |
32 | | |
33 | | #[derive(Clone, Debug)] |
34 | | pub struct ObjectStore<T> { |
35 | | objects: Vec<Rc<T>>, |
36 | | objects_by_name: IndexMap<Rc<T>>, |
37 | | objects_imports: HashMap<String, Rc<T>>, |
38 | | } |
39 | | |
40 | | impl<T: bp3d_util::index_map::Index<Key = str>> Default for ObjectStore<T> { |
41 | 0 | fn default() -> Self { |
42 | 0 | Self::new() |
43 | 0 | } |
44 | | } |
45 | | |
46 | | impl<T: bp3d_util::index_map::Index<Key = str>> ObjectStore<T> { |
47 | 275 | pub fn new() -> Self { |
48 | 275 | Self { |
49 | 275 | objects: Vec::new(), |
50 | 275 | objects_by_name: IndexMap::new(), |
51 | 275 | objects_imports: HashMap::new(), |
52 | 275 | } |
53 | 275 | } |
54 | | |
55 | 316 | pub fn iter(&self) -> impl Iterator<Item = &Rc<T>> { |
56 | 316 | self.objects.iter() |
57 | 316 | } |
58 | | |
59 | 212 | pub fn get(&self, name: &str) -> Option<&Rc<T>> { |
60 | 212 | self.objects_by_name.get(name).or_else(|| self.objects_imports.get(name)108 ) |
61 | 212 | } |
62 | | |
63 | 118 | pub fn insert(&mut self, obj: Rc<T>) { |
64 | 118 | self.objects_by_name.insert(obj.clone()); |
65 | 118 | self.objects.push(obj); |
66 | 118 | } |
67 | | |
68 | 38 | pub fn insert_import(&mut self, import_name: String, obj: Rc<T>) { |
69 | 38 | self.objects_imports.insert(import_name, obj); |
70 | 38 | } |
71 | | } |
72 | | |
73 | | macro_rules! name_index { |
74 | | ($t: ty => $key: ident) => { |
75 | | impl crate::compiler::util::types::Name for $t { |
76 | 678 | fn name(&self) -> &str { |
77 | 678 | &*self.$key |
78 | 678 | } |
79 | | } |
80 | | |
81 | | impl bp3d_util::index_map::Index for $t { |
82 | | type Key = str; |
83 | | |
84 | 251 | fn index(&self) -> &Self::Key { |
85 | 251 | &*self.$key |
86 | 251 | } |
87 | | } |
88 | | }; |
89 | | } |
90 | | |
91 | | pub(crate) use name_index; |